<template>
    <div class="upload-question">
        <el-dialog v-model="dialogVisible" class="review-dialog" width="615" :show-close="false"
            :close-on-click-modal="false" :close-on-press-escape="false">
            <div class="header">
                <div class="title">上传题目</div>
                <div class="header-right">
                    <div class="operate">
                        <div class="btn primary" @click="uploadFile">上传</div>
                    </div>
                    <div class="select">
                        <el-select class="select-border-hidden" v-model="uploadType" style="width: 120px">
                            <el-option v-for="item in uploadOptions" :key="item.value" :label="item.label"
                                :value="item.value" />
                        </el-select>
                    </div>
                    <div class="close" @click="close">
                        <i class="iconfont icon-chahao1 close-dialog"></i>
                    </div>
                </div>
            </div>
            <div class="content">
                <div class="file-list" v-if="uploadFileList.length != 0">
                    <div class="file-item" v-for="(item, index) in uploadFileList" :key="item.md5">
                        <div class="file-head">
                            <div class="file-left">
                                <div class="fileName">{{ item.name }}</div>
                                <div class="fileSize">{{ formatSize(item.size) }}</div>
                            </div>
                            <div class="upload-type">上传类型：{{ uploadType }}</div>
                        </div>
                        <div class="file-right">
                            <el-popconfirm class="box-item" title="确定删除已上传的文件吗？" placement="bottom"
                                @confirm="deleteUpload(item)">
                                <template #reference>
                                    <div class="btn danger">删除</div>
                                </template>
                            </el-popconfirm>
                        </div>
                    </div>
                </div>
                <div class="empty" v-else>
                    <i class="iconfont icon-kongzhuangtai"></i>
                    <div class="empty-text">暂无上传成功文件，去<span class="tips" @click="uploadFile">上传文件</span>吧~</div>
                </div>
            </div>
            <div class="footer">
                <div class="btn confirm" @click="confirm">确定</div>
                <div class="btn cancel" @click="close">取消</div>
            </div>
        </el-dialog>
        <ojUploadFile ref="ojUpload" @uploadAllFinish="uploadAllFinish" :uploadType="getUploadType"></ojUploadFile>
    </div>
</template>
<script setup>
import { ref, onMounted, watch, computed, nextTick } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
    formatSize,
} from "@/utils/global.js";
import { importProblemAPI } from '@/api/adminAPI/adminAPI.js'
import ojUploadFile from "@/components/common/ojUploadFile.vue";
import { ElMessage } from "element-plus";


const emit = defineEmits(['regetQuestionList', 'showTask'])

const uploadType = ref('HOJ')
const ojUpload = ref(null)
const dialogVisible = ref(false);
const uploadFileList = ref([])
const taskList = ref([])
const uploadOptions = [
    { label: 'HOJ', value: 'HOJ' },
    { label: 'FPS', value: 'FPS' },
    { label: 'QDOJ', value: 'QDOJ' },
    { label: 'JSON', value: 'JSON' },
    { label: 'ZIP', value: 'ZIP' },
]

const uploadTypeList = {
    HOJ: ["ZIP"],
    FPS: ["XML"],
    QDOJ: ["ZIP"],
    JSON: ["JSON"],
    ZIP: ["ZIP"],
};

const getUploadType = computed(() => {
    return uploadTypeList[uploadType.value];
});

const open = (data = null) => {
    dialogVisible.value = true;
};

const close = () => {
    dialogVisible.value = false;
    uploadType.value = 'HOJ'
    uploadFileList.value = []
};
const uploadAllFinish = (data) => {
    // 1. 添加新数据
    uploadFileList.value.push(...data);
    console.log(uploadFileList.value);

    // 2. 过滤重复md5，保留最后出现的
    uploadFileList.value = uploadFileList.value
        .filter(file => file.ossName) // 先过滤有ossName的
        .filter((file, index, self) =>
            index === self.findLastIndex(f => f?.md5 === file?.md5)
        );
}
const confirm = async () => {
    if (uploadFileList.value.length <= 0) {
        ElMessage.error('暂无已上传的文件，快去上传吧~')
        return
    }
    for (let i = 0; i < uploadFileList.value.length; i++) {
        await uploadFinish(uploadFileList.value[i])
    }
    emit('showTask', taskList.value)
    nextTick(() => {
        close()
    })
}
const deleteUpload = (data) => {
    const index = uploadFileList.value.findIndex(item => {
        return item.md5 == data.md5
    })
    if (index != -1) {
        uploadFileList.value.splice(index, 1)
    }
}
const uploadFinish = async (data) => {
    let postData = {
        importType: uploadType.value,
    }
    postData.ossName = data.ossName;
    try {
        const response = await importProblemAPI(postData)
        const { code, data, msg } = response.data
        if (code == 200) {
            emit('regetQuestionList')
            taskList.value.push(data)
        }
    } catch (err) {
        console.log(err);
    }
}
const uploadFile = () => {
    ojUpload.value.open()
}

watch(
    () => uploadType.value,
    () => {
        uploadFileList.value = []
    }
)

defineExpose({
    open,
});
</script>
<style lang="less" scoped>
.upload-question {
    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 10px;

        .title {
            line-height: 33px;
            font-weight: 700;
            font-size: 24px;
            color: #222;
        }

        .header-right {
            display: flex;
            align-items: center;
            justify-content: center;
            margin-top: 4px;

            .operate {
                display: flex;
                align-items: center;
                justify-content: center;
                margin-right: 10px;

                .btn {
                    width: 80px;
                    height: 30px;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    transition: all 0.3s ease-in-out;
                    color: #fff;
                    cursor: pointer;
                    border-radius: 4px;

                    &:hover {
                        opacity: 0.8;
                    }
                }

                .primary {
                    background-color: #4e59e8;
                }
            }

            .select {
                ::v-deep(.select-border-hidden) {
                    border-bottom: 1px solid #8c8c8c !important;
                    border-radius: 4px;
                }
            }

            .close {
                margin-left: 10px;

                .close-dialog {
                    cursor: pointer;
                    font-size: 34px;
                    color: #8c8c8c;
                    transition: all 0.3s ease-in-out;

                    &:hover {
                        color: #8c8c8cbd;
                    }
                }
            }
        }

    }

    .content {
        height: 410px;
        padding: 10px 5px;
        overflow: auto;
        scrollbar-width: none;
        /* firefox */
        -ms-overflow-style: none;
        /* IE 10+ */
        overflow-x: hidden;
        overflow-y: auto;

        .empty {
            text-align: center;

            .iconfont {
                font-size: 90px;
            }

            .empty-text {
                font-size: 16px;

                .tips {
                    cursor: pointer;
                    color: #4e59e8;
                    transition: all 0.3s ease-in-out;

                    &:hover {
                        opacity: 0.8;
                    }
                }
            }
        }

        .file-item {
            margin-top: 10px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-radius: 4px;
            box-shadow: 0px 0px 10px #d6d6d6;
            padding: 10px 20px;

            .file-left {
                display: flex;
                align-items: center;

                .fileName {
                    font-size: 16px;
                    color: #555;
                    margin-right: 10px;
                }

                .fileSize {
                    font-size: 14px;
                    color: #8c8c8c;
                }
            }

            .upload-type {
                font-size: 14px;
                color: #8c8c8c;
            }

            .file-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
                gap: 10px;

                .btn {
                    color: #fff;
                    padding: 5px 10px;
                    border-radius: 4px;
                    cursor: pointer;
                    transition: all 0.3s ease-in-out;

                    &:hover {
                        opacity: 0.8;
                    }
                }

                .primary {
                    background-color: #409eff;
                }

                .danger {
                    background-color: #ff4040;
                }
            }
        }
    }

    .footer {
        display: flex;
        align-items: center;
        justify-content: center;
        flex-wrap: wrap;
        gap: 10px;

        .btn {
            width: 100px;
            height: 32px;
            font-size: 15px;
            color: #fff;
            border-radius: 4px;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s ease-in-out;
            cursor: pointer;

            &:hover {
                opacity: 0.8;
            }
        }

        .confirm {
            background-color: #5f47ea;
        }

        .cancel {
            background-color: #f43c2f;
        }
    }
}
</style>
